package com.ycfmedical.bed.model;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XFragment;

/**
 * 操作LineChart的管理类
 * Created by Administrator on 2017-03-24.
 */

public class ChartManage {
    private ArrayList<LineChart> lineCharts = new ArrayList<LineChart>();

    public ArrayList<LineChart> getLineCharts() {
        return lineCharts;
    }

    public void addChart(LineChart lineChart) {
        initChart(lineChart);
        lineCharts.add(lineChart);
    }
    public void addChart(LineChart lineChart,boolean enabled) {
        initChart(lineChart);
        lineCharts.add(lineChart);
        lineChart.setTouchEnabled(enabled);
    }

    private void initChart(LineChart lineChart) {
        /**
         * 在这里面主要初始化lineChart的属性。
         * */
        lineChart.getLegend().setEnabled(false);        //左下角标签不显示
        lineChart.setTouchEnabled(true);                //禁止点击事件
        Description description = new Description();
        description.setText("");
        lineChart.setDescription(description);
        lineChart.setDoubleTapToZoomEnabled(false);
        lineChart.setHighlightPerDragEnabled(false);
    }

    public void setLineData(ArrayList<ArrayList<Entry>> data) {
        for (int i = 0; i < data.size(); i++) {
            lineCharts.get(i).setData(new LineData(new LineDataSet(data.get(i), "")));
            lineCharts.get(i).getLineData().setDrawValues(false);        //所有文本不绘制
        }
    }

    public void setLineData(int Index, ArrayList<Entry> data,boolean flag) {
        LineDataSet lineDataSet = new LineDataSet(data, "");
        if(flag){
            lineDataSet.setDrawFilled(true);
            lineDataSet.setFillColor(Color.rgb(103,181,235));
        }
        lineCharts.get(Index).setData(new LineData(lineDataSet));
        lineCharts.get(Index).getLineData().setDrawValues(false);        //所有文本不绘制
        lineCharts.get(Index).notifyDataSetChanged();
        lineCharts.get(Index).invalidate();
    }
    /**
     * 翻转Y轴
     * */
    public void inverted(int Index,boolean flag){
        lineCharts.get(Index).getAxisRight().setInverted(flag);
        lineCharts.get(Index).getAxisLeft().setInverted(flag);
    }

    public void setLine(int size, int lineColor, boolean DrawCircles, LineDataSet.Mode mode) {
        LineDataSet dataSet;
        dataSet = (LineDataSet) lineCharts.get(size).getData().getMaxEntryCountSet();
        dataSet.setColor(lineColor);
        dataSet.setDrawCircles(DrawCircles);
        dataSet.setMode(mode);
        dataSet = null;
    }

    public void setbackground(int size, int color) {
        lineCharts.get(size).setBackgroundColor(color);
    }

    /**
     * 设置x缩放的最小最大值
     */
    public void setMinMaxScaleX(int Index, float minScaleX, float maxScaleX) {
        lineCharts.get(Index).getViewPortHandler().setMinMaxScaleX(minScaleX, maxScaleX);
    }

    /**
     * 设置Y缩放的最小最大值
     */
    public void setMinMaxScaleY(int Index, float minScaleY, float maxScaleY) {
        lineCharts.get(Index).getViewPortHandler().setMinMaxScaleY(minScaleY, maxScaleY);
    }

    public void setX(int size, float Xmin, float Xmax, boolean enabled) {
        lineCharts.get(size).getXAxis().setEnabled(enabled);
        lineCharts.get(size).getXAxis().setAxisMinimum(Xmin);
        lineCharts.get(size).getXAxis().setAxisMaximum(Xmax);
    }

    public void setY(int size, float Ymin, float Ymax, boolean enabled) {
        lineCharts.get(size).getAxisLeft().setAxisMinimum(Ymin);
        lineCharts.get(size).getAxisRight().setAxisMaximum(Ymax);

        lineCharts.get(size).getAxisLeft().setAxisMinimum(Ymin);
        lineCharts.get(size).getAxisLeft().setAxisMaximum(Ymax);

        lineCharts.get(size).getAxisLeft().setEnabled(enabled);
        lineCharts.get(size).getAxisRight().setEnabled(enabled);
    }

    public LimitLine getLimitLine(int value, String cue, int lineColor, int textColor,LimitLine.LimitLabelPosition post) {
        LimitLine ll = new LimitLine(value, cue);
        ll.setLineColor(lineColor);
        ll.setTextColor(textColor);
        ll.setLineWidth(1f);
        ll.setTextSize(10f);
        ll.isEnabled();
        ll.setLabelPosition(post);
        return ll;
    }
    public static void setXText(LineChart lineChart, int num, IAxisValueFormatter xf){
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setLabelCount(num);
        xAxis.setValueFormatter(xf);

    }

}
